package com.cyt.web.controller.phone;

import com.cyt.base.model.dto.ReturnResult;
import com.cyt.base.util.BaseConstant;
import com.cyt.base.util.StringUtils;
import com.cyt.sys.model.po.SysFile;
import com.cyt.sys.model.po.SysUser;
import com.cyt.sys.model.vo.SysFileVo;
import com.cyt.sys.service.SysFileService;
import com.cyt.web.controller.base.PhoneController;
import com.cyt.work.kwmanagement.model.po.KwManagement;
import com.cyt.work.kwmanagement.model.vo.KwManagementVo;
import com.cyt.work.kwmanagement.model.vo.KwStudentMessageVo;
import com.cyt.work.kwmanagement.model.vo.KwTeacherMessageVo;
import com.cyt.work.kwmanagement.service.KwManagementService;
import com.cyt.work.kwmanagement.service.KwStudentMessageService;
import com.cyt.work.kwmanagement.service.KwStudentSignService;
import com.cyt.work.kwmanagement.service.KwTeacherMessageService;
import com.cyt.wx.model.po.WxEducationUser;
import com.cyt.wx.model.po.WxMember;
import com.cyt.wx.model.vo.WxEducationUserVo;
import com.cyt.wx.service.WxEducationUserService;
import org.apache.bcel.generic.NEW;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2019/12/11.
 */
@RestController
public class PhoneKwManageController  extends PhoneController {

    /**
     * 考务管理
     *
     * @author han jun
     * @date 2018/12/15 11:26
     */
        @Autowired
        private WxEducationUserService wxEducationUserService;

        @Autowired
        private KwTeacherMessageService kwTeacherMessageService;

       @Autowired
        private KwStudentMessageService kwStudentMessageService;

       @Autowired
       private KwStudentSignService kwStudentSignService;

       @Autowired
       private SysFileService sysFileService;

       @Resource
       private KwManagementService kwManagementService;



        /**
         * 登录页面
         * @param token
         * @param applyId
         * @author han jun
         * @date 2019/9/4 14:55
         */
        @RequestMapping(value = "/phone/kwmanagement/kwmanagementLogin")
        public ModelAndView questionLogin(String token, String applyId) {
            ModelAndView modelAndView = new ModelAndView("/mobile/kwmanagement/kwManagementLogin");
            modelAndView.addObject("token", token);
            modelAndView.addObject("applyId", applyId);
            return modelAndView;
        }
        /**
         * 登录
         * @param token
         * @param applyId
         * @param loginName
         * @param password
         * @author  hanjun
         * @date 2019/9/4 16:26
         */
        @RequestMapping(value = "/phone/kwmanagement/login",method = RequestMethod.POST)
        public ReturnResult login(String token, String applyId, String loginName, String password){
            WxMember wxMember = acceptToken(token);
            return wxEducationUserService.login(wxMember, loginName, password);
        }

        /**
         * 考务详情界面
         * @param token
         * @param applyId
         * @author cuibiming
         * @date 2019/9/9 08:50
         */
        @RequestMapping(value = "/phone/kwmanage/kwmanageDetail")
        public ModelAndView associationList(String token, String applyId) {
            // 校验
            Map<String, Object> map = acceptToken(token, applyId, APPLY_TYPE_STZX);
            if (!(Boolean) map.get("flag")) {
                return (ModelAndView) map.get("modelAndView");
            }
            WxMember wxMember = acceptToken(token);
            WxEducationUserVo wxEducationUserVo = new WxEducationUserVo();
            wxEducationUserVo.setMemberId(wxMember.getId());
            List<WxEducationUser> wxEducationUserList = wxEducationUserService.listExample(wxEducationUserVo);
            //能登录进去就可以直接使用
            WxEducationUser wxEducationUser = wxEducationUserList.get(0);
            ModelAndView modelAndView =new ModelAndView();
            if(wxEducationUser.getUserType().equals(BaseConstant.EDUCATION_USER_TYPE_TEACHER)){
                modelAndView = new ModelAndView("/mobile/kwmanagement/teacherKwmanagement");
            }else if(wxEducationUser.getUserType().equals(BaseConstant.EDUCATION_USER_TYPE_STUDENT)) {
                modelAndView = new ModelAndView("/mobile/kwmanagement/studentKwmanagement");
                //传递一个照片审核的状态去分析
                if (StringUtils.isEmpty(wxEducationUser.getExamine())){
                    modelAndView.addObject("msg", "暂无消息");
                }else if  (wxEducationUser.getExamine().equals("0")){
                    modelAndView.addObject("msg", "您的一寸照片审核通过了");
                }else if  (wxEducationUser.getExamine().equals("1")){
                    modelAndView.addObject("msg", "您的一寸照片未被通过");
                }else if  (wxEducationUser.getExamine().equals("2")){
                    modelAndView.addObject("msg", "您的一寸照片正在等待审核");
                }
                modelAndView.addObject("msgType", wxEducationUser.getExamine());

            }

            modelAndView.addObject("token", token);
            modelAndView.addObject("applyId", applyId);
            modelAndView.addObject("name", wxEducationUser.getName());
            return modelAndView;
        }



    /**
     *教师考务安排详情页
     *
     * @param token
     * @param applyId
     * @return ModelAndView
     * @author Jun Han
     * @date 2019/12/11 9:58
     */
    @RequestMapping(value = "/phone/kwmanagement/kwTeacherDetail")
    public ModelAndView kwTeacherDetail(String token, String applyId,String type) {
        ModelAndView modelAndView = new ModelAndView("/mobile/kwmanagement/teacherKwDetail");
        modelAndView.addObject("token", token);
        modelAndView.addObject("applyId", applyId);
        modelAndView.addObject("type", type);
        return modelAndView;
    }


    /**
     *教师考试签到列表
     *
     * @param token
     * @param applyId
     * @return ModelAndView
     * @author Jun Han
     * @date 2019/12/11 9:58
     */
    @RequestMapping(value = "/phone/kwmanagement/kwTeacherSignList")
    public ModelAndView kwTeacherSignList(String token, String applyId,String type) {
        ModelAndView modelAndView = new ModelAndView("/mobile/kwmanagement/teacherKwSign");
        modelAndView.addObject("token", token);
        modelAndView.addObject("applyId", applyId);
        modelAndView.addObject("type", type);
        return modelAndView;
    }

    /**
     *教师考试签到详情界面
     *
     * @param token
     * @param applyId
     * @return ModelAndView
     * @author Jun Han
     * @date 2019/12/11 9:58
     */
    @RequestMapping(value = "/phone/kwmanagement/kwTeacherSignDetail")
    public ModelAndView kwTeacherSignDetail(String token, String applyId,String ksId,String kcId,String signType,String kmId) {
        ModelAndView modelAndView = new ModelAndView("/mobile/kwmanagement/teacherKwSigndetail");
        modelAndView.addObject("token", token);
        modelAndView.addObject("applyId", applyId);
        modelAndView.addObject("ksId", ksId);
        modelAndView.addObject("kcId", kcId);
        modelAndView.addObject("signType", signType);
        modelAndView.addObject("kmId", kmId);
        return modelAndView;
    }


    /**
     *教师考试签到提交页面
     *
     * @param token
     * @param applyId
     * @return ModelAndView
     * @author Jun Han
     * @date 2019/12/11 9:58
     */
    @RequestMapping(value = "/phone/kwmanagement/kwTeacherSignComment")
    public ModelAndView kwTeacherSignComment(String token, String applyId,String ksId,String kcId,String signType,String kmId) {
        ModelAndView modelAndView = new ModelAndView("/mobile/kwmanagement/teacherKwSignCommit");
        modelAndView.addObject("token", token);
        modelAndView.addObject("applyId", applyId);
        modelAndView.addObject("ksId", ksId);
        modelAndView.addObject("kcId", kcId);
        modelAndView.addObject("signType", signType);
        modelAndView.addObject("kmId", kmId);
        return modelAndView;
    }





    /**
     *获取教师巡考监考列表(按照时间分配)
     *
     * @param token
     * @param applyId
     * @return ModelAndView
     * @author Jun Han
     * @date 2019/12/11 9:58
     */
    @RequestMapping(value = "/phone/kwmanagement/getTeacherKwList")
    public ReturnResult getTeacherKwList(String token,String applyId,String type) {
        WxMember wxMember = acceptToken(token);
        WxEducationUser wxEducationUser = wxEducationUserService.selectEducationUserByMemberId(wxMember.getId());
        KwTeacherMessageVo kwTeacherMessageVo = new KwTeacherMessageVo();
        kwTeacherMessageVo.setTeacherId(wxEducationUser.getId());
        kwTeacherMessageVo.setType(type);
        return kwTeacherMessageService.findVoPageNew(kwTeacherMessageVo);
    }

    /**
     *获取教师监考列表
     *
     * @param token
     * @param applyId
     * @return ModelAndView
     * @author Jun Han
     * @date 2019/12/11 9:58
     */
    @RequestMapping(value = "/phone/kwmanagement/getTeacherJKList")
    public ReturnResult getTeacherJKList(String token,String applyId,String type) throws ParseException {
        WxMember wxMember = acceptToken(token);
        WxEducationUserVo wxEducationUserVo = new WxEducationUserVo();
        wxEducationUserVo.setMemberId(wxMember.getId());
        List<WxEducationUser> wxEducationUserList = wxEducationUserService.listExample(wxEducationUserVo);
        KwTeacherMessageVo kwTeacherMessageVo = new KwTeacherMessageVo();
        kwTeacherMessageVo.setTeacherId(wxEducationUserList.get(0).getId());
        kwTeacherMessageVo.setType(type);
        return kwTeacherMessageService.findVoPageSign(kwTeacherMessageVo);
    }

    /**
     * 获取学生签到明细表
     *
     * @param kwStudentMessageVo
     * @return ReturnResult
     * @author Jun Han
     * @date 2019/12/4 15:36
     */
    @RequestMapping(value = "/phone/kwmanagement/studentSignListNew", method = RequestMethod.GET)
    public ReturnResult studentSignListNew(KwStudentMessageVo kwStudentMessageVo) {
        ReturnResult returnResult = kwStudentMessageService.studentSignList(kwStudentMessageVo);
        return  returnResult;
    }



    /**
     * 批量保存签到信息
     *
     * @param kwKmSignMessageJson
     * @param applyId
     * @return ReturnResult
     * @author Jun Han
     * @date 2019/12/4 15:36
     */
    @RequestMapping(value = "/phone/kwmanagement/studentSignMessage", method = RequestMethod.POST)
    public ReturnResult studentSignListNew(String applyId ,String kwKmSignMessageJson,String token) {
        WxMember wxMember = acceptToken(token);
        ReturnResult returnResult = kwStudentSignService.addOrUpdateAll(applyId,kwKmSignMessageJson,wxMember);
        return  returnResult;
    }


    /**
     *学生的准考证界面
     *
     * @param token
     * @param applyId
     * @return ModelAndView
     * @author Jun Han
     * @date 2019/12/11 9:58
     */
    @RequestMapping(value = "/phone/kwmanagement/studentAttence")
    public ModelAndView studentAttence(String token, String applyId,String picUrl) {
        ModelAndView modelAndView = new ModelAndView("/mobile/kwmanagement/studentExamAttendance");
        modelAndView.addObject("token", token);
        modelAndView.addObject("applyId", applyId);
        modelAndView.addObject("picUrl", picUrl);
        return modelAndView;
    }


    /**
     *学生的首页
     *
     * @param token
     * @param applyId
     * @return ModelAndView
     * @author Jun Han
     * @date 2019/12/11 9:58
     */
    @RequestMapping(value = "/phone/kwmanagement/studentMian")
    public ModelAndView studentMian(String token, String applyId) {
        ModelAndView modelAndView = new ModelAndView("/mobile/kwmanagement/studentKwmanagement");
        modelAndView.addObject("token", token);
        modelAndView.addObject("applyId", applyId);
        return modelAndView;
    }


    /**
     *学生提交照片审核界面
     *
     * @param token
     * @param applyId
     * @return ModelAndView
     * @author Jun Han
     * @date 2019/12/11 9:58
     */
    @RequestMapping(value = "/phone/kwmanagement/studentOnePicture")
    public ModelAndView studentOnePicture(String token, String applyId) {
        ModelAndView modelAndView = new ModelAndView("/mobile/kwmanagement/studentOnePicture");
        WxMember wxMember = acceptToken(token);
        WxEducationUser wxEducationUser = wxEducationUserService.selectEducationUserByMemberId(wxMember.getId());
        //获取图片的路径
        SysFileVo sysFileVo = new SysFileVo();
        sysFileVo.setObjectId(wxEducationUser.getObjectidOne());
        List<SysFile> list;
        String str ="";
        if (StringUtils.isEmpty(wxEducationUser.getObjectidOne())){
        }else {
            list = (List<SysFile>) sysFileService.list(sysFileVo).getData();
            for (SysFile sysFile:list){
                sysFile.setPath("/sysFile/showFileByPath?path="+sysFile.getPath());
                str = "/sysFile/showFileByPath?path="+sysFile.getPath();
            }
        }

        modelAndView.addObject("token", token);
        modelAndView.addObject("applyId", applyId);
        modelAndView.addObject("imgurl",str);
        modelAndView.addObject("infoPhoto",wxEducationUser.getObjectidOne());
        if (StringUtils.isEmpty(wxEducationUser.getExamine())){
            modelAndView.addObject("msg", "请上传照片");
        }else if  (wxEducationUser.getExamine().equals("0")){
            modelAndView.addObject("msg", "审核通过了");
        }else if  (wxEducationUser.getExamine().equals("1")){
            modelAndView.addObject("msg", "未被通过");
        }else if  (wxEducationUser.getExamine().equals("2")){
            modelAndView.addObject("msg", "正在审核");
        }

        return modelAndView;
    }

    /**
     * 获取学生准考证的信息
     *
     * @param token
     * @return ReturnResult
     * @author Jun Han
     * @date 2019/12/4 15:36
     */
    @RequestMapping(value = "/phone/kwmanagement/studentSignMessageNew", method = RequestMethod.GET)
    public ReturnResult studentSignListNew(String applyId,String token) {
        WxMember wxMember = acceptToken(token);
        WxEducationUser wxEducationUser = wxEducationUserService.selectEducationUserByMemberId(wxMember.getId());
        //获取所有发布的考试
        KwManagementVo kwManagementVo = new KwManagementVo();
        kwManagementVo.setState("1");
        List<KwManagement> list = kwManagementService.listExample(kwManagementVo);
        KwStudentMessageVo kwStudentMessageVo = new KwStudentMessageVo();
        kwStudentMessageVo.setKsId(list.get(0).getId());
        ReturnResult returnResult = kwStudentMessageService.getExamAttance(kwStudentMessageVo,wxEducationUser);
        return  returnResult;
    }

    /**
     * 提交一寸照片审核
     *
     * @param token
     * @return ReturnResult
     * @author Jun Han
     * @date 2019/12/4 15:36
     */
    @RequestMapping(value = "/phone/kwmanagement/upStudentOneImage", method = RequestMethod.POST)
    public ReturnResult studentSignListNew(String applyId,String token,WxEducationUserVo wxEducationUserVo) {
        WxMember wxMember = acceptToken(token);
        WxEducationUserVo wxEducationUserVo1 = new WxEducationUserVo();
        wxEducationUserVo1.setMemberId(wxMember.getId());
        WxEducationUser wxEducationUser = wxEducationUserService.listExample(wxEducationUserVo1).get(0);
        wxEducationUserVo.setId(wxEducationUser.getId());
        SysUser sysUser = new SysUser();
        sysUser.setId(wxMember.getId());
        wxEducationUserService.updateOneImage(wxEducationUserVo,sysUser);
        return new  ReturnResult();
    }









}
